package com.servlet;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import hyl.base.cache.redis.CRedis;
import hyl.base.cache.redis.RdMapString;
import hyl.base.cache.redis.RedisPool;
import hyl.base.flow.Flow;
import hyl.base.flow.PoolFlow;
import hyl.core.Amy;
import hyl.core.MyFun;
import hyl.core.db.MyDB;
import hyl.core.run.MyTimer;
import hyl.ext.base.MySession;
import hyl.ext.base.SessionFactory;
import hyl.ext.web.ms.ABOpen;
import hyl.ext.web.ms.TMsA;

import redis.clients.jedis.Pipeline;

public class My extends Amy {
	protected static My my;
	public static RdMapString _rd;

	// public static String DB2 = "mysql2";
	static {
		DB1 = "mysql1";

	}
	static TMsA msA;

	// protected static MyKafka _kfkp;
	// public static MyDB _timerdb1= MyDB.createCommitDB(DB1);
	private My() {
		// MyPath.test(MyPath.class);
		M.LRole = M2.getRoles();// 把所有会员的权限读取出来,不适合大用户的表
		SessionFactory.getInstance();
		msA = TMsA.getInstance();
		msA.init();
		msA.on收到消息处理=(ng)->{
			if (ng.msgs[0].equals("MYSESSION")) {
				String appidb=ng.msgs[1];
				String useridb=ng.msgs[2];
			//	String siddb=ng.msgs[3];
			//	BInfo bifo=msA.getBInfo(appidb);
				ABOpen abopen=	msA.getABOpen2(appidb, MyFun.str2int(useridb) );
				if (abopen==null) {
					abopen= 	new ABOpen();
					
					}
				//msA.updateABOpen
			}
		};
		// _rd = new RdMapString("redis1",11);

	}

	public static TMsA getMsA() {
		return msA;
	}

	/*
	 * public static MyKafka getKfkP() { return _kfkp; }
	 */
	/**
	 * my 实例化 启动
	 * 
	 * @return
	 */
	public static My getInstance() {
		// String path=MyPath.getLastPathOfClassLoader(My.class.getClassLoader());
		// MyConfig conf=MyConfig.deserial(path);
		if (my == null) {
			my = new My();
		}
		// 嵌入微服务组件,监听各个微服务发来的心跳

		// MyConfig.serial(path);
		// _kfkp = MyKafka.getInsProducer();

		return my;
	}

	public static MyDB createCommitDb2() {
		// 通过连接创建DB对象 操作sql
		return MyDB.createCommitDB(DB1);
	}

	public static void reset() {
		my = new My();
		// _rewardService = new RewardService2();
		MyTimer.close("日同步");
	}

	// 判断权限
	public static void checkRole(String JSPTITLE, HttpServletRequest req, HttpServletResponse res) throws IOException {
		MySession ses = getSession(req);
		String quanxian = ses.tag.get("quanxian").toString();
		if (quanxian.indexOf(JSPTITLE.trim() + ",") < 0) {
			res.sendRedirect(req.getContextPath() + M.URL_404);
		}
	}

	/**
	 * 适用于直接访问的jsp页面 实例化相当于session的初始化
	 */
	public static MySession getSession(HttpServletRequest req, HttpServletResponse res) {
		return SessionFactory.getSession(req, res);
	}

	/**
	 * 适用于sping control
	 */
	public static MySession getSession(HttpServletRequest req) {
		return SessionFactory.getSession(req);
	}

	/**
	 * 适用于直接访问的jsp页面
	 */
	public static int getMySessionUserID(HttpServletRequest req, HttpServletResponse res) {
		MySession ses = getSession(req, res);
		return ses.getUserId();
	}

	// 2019
	public static int getLanguage(HttpServletRequest req, HttpServletResponse res) {
		return SessionFactory.getSession(req, res).getLang();
	}

	/**
	 * 适用于直接访问的jsp页面 2019.3.5 sx
	 */
	public static int getMySessionRoleid(HttpServletRequest req, HttpServletResponse res) {
		MySession ses = getSession(req, res);
		return ses.getRoleid();
	}

	// 2019
	public static int getMySessionRoleId(HttpServletRequest req) {
		MySession ses = getSession(req);
		return ses.getRoleid();
	}

	// 2019
	public static int getMySessionUserID(HttpServletRequest req) {
		MySession ses = getSession(req);
		return ses.getUserId();
	}

	// 2019
	public static MySession getSession(String uuid) {
		return SessionFactory.getSessionById(uuid);
	}

	public synchronized static Pipeline getRd1(int idx) {
		// 通过连接创建DB对象 操作sql
		return RedisPool.getInstance().getRedis("redis1", idx).createPipeline();
	}

	public synchronized static CRedis createRd1(int idx) {
		// 通过连接创建DB对象 操作sql
		return RedisPool.getInstance().getRedis("redis1", idx);
	}

	static PoolFlow fp;

	public static PoolFlow getfp() {
		if (fp == null)
			fp = PoolFlow.getInstance();
		return fp;
	}

	public static Flow get角色调整流程() {
		Flow model = getfp().getFlowModel("应用流程");
		return model;
	}

	public static void exit() {
		closeAllThreads();
		closeDb();
		closeRd();
	}

	private static void closeRd() {
		RedisPool.getInstance().close();
		Log.info("redis 连接池正常关闭...");
	}

	public static void onErrClose() {
		onExit = new Runnable() {
			public void run() {
				Log.info("异常关闭触发事件");
				exit();
			}
		};
	}

}
